public class getIntersectionNode {
    /**
     * 处理相交链表
     */
    public class ListNode{
        int val;
        ListNode next;
        ListNode(int x){
            val=x;
            next=null;
        }
    }

    public static void main(String[] args) {
        getIntersectionNode solution = new getIntersectionNode();

        // 创建示例相交链表
        ListNode commonNode = solution.new ListNode(8);
        ListNode headA = solution.new ListNode(4);
        headA.next = solution.new ListNode(1);
        headA.next.next = commonNode;
        ListNode headB = solution.new ListNode(5);
        headB.next = solution.new ListNode(6);
        headB.next.next = solution.new ListNode(1);
        headB.next.next.next = commonNode;

        // 调用方法求解
        ListNode result = solution.method(headA, headB);

        // 输出结果
        System.out.println(result.val); // 输出：8，表示两个链表相交的节点的值为8
    }

    public ListNode method(ListNode headA,ListNode headB){
        if(headA == null || headB==null){
            return null;
        }
        ListNode pA=headA,pB=headB;
        while(pA!=pB){
            if(pA==null){
                pA=headB;
            }else{
               pA=pA.next;
            }
            if(pB==null){
                pB=headA;
            }else{
                pB=pB.next;
            }
        }
        return pA;
    }
}
